package com.songlanyun.modules.distributionconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.distributionconfig.model.DistributionShopConfigDTO;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.distributionconfig.dao.DistributionShopConfigDao;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopConfig;
import com.songlanyun.modules.distributionconfig.service.DistributionShopConfigService;
import org.springframework.transaction.annotation.Transactional;


@Service("distributionShopConfigService")
public class DistributionShopConfigServiceImpl extends ServiceImpl<DistributionShopConfigDao, DistributionShopConfig> implements DistributionShopConfigService {

    @Override
    public DistributionShopConfig create(DistributionShopConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public DistributionShopConfig modify(DistributionShopConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DistributionShopConfig> page = this.page(
                new Query<DistributionShopConfig>().getPage(params),
                new QueryWrapper<DistributionShopConfig>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取商家自营商品统一分销设置
     */
    @Override
    public DistributionShopConfig getByShop(Long shopId) {
       return  this.getOne(new LambdaQueryWrapper<DistributionShopConfig>()
                .eq(DistributionShopConfig::getShopId, shopId)
                .last("limit 1"));
    }

    /**
     * 保存商家自营商品统一分销设置
     * @param shopId
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public DistributionShopConfig set(Long shopId, DistributionShopConfigDTO dto) {
        DistributionShopConfig config = this.getByShop(shopId);
        if(config == null)
        {
            DistributionShopConfig distributionShopConfig = new DistributionShopConfig();
            distributionShopConfig.setOpen(dto.getOpen());
            distributionShopConfig.setShopId(shopId);
            distributionShopConfig.setComputeMode(GoodsConstant.ComputeMode.getByCode(dto.getComputeMode()));
            distributionShopConfig.setFirstLevelPrice(dto.getFirstLevelPrice());
            distributionShopConfig.setSecondLevelPrice(dto.getSecondLevelPrice());
            distributionShopConfig.setFirstLevelRatio(dto.getFirstLevelRatio());
            distributionShopConfig.setSecondLevelRatio(dto.getSecondLevelRatio());
            this.create(distributionShopConfig);
            return distributionShopConfig;
        }
        else
        {
            config.setComputeMode(GoodsConstant.ComputeMode.getByCode(dto.getComputeMode()));
            config.setOpen(dto.getOpen());
            config.setFirstLevelPrice(dto.getFirstLevelPrice());
            config.setSecondLevelPrice(dto.getSecondLevelPrice());
            config.setFirstLevelRatio(dto.getFirstLevelRatio());
            config.setSecondLevelRatio(dto.getSecondLevelRatio());
            this.modify(config);
            return config;
        }

    }
}